/*******************************************************************************
 * Copyright (c) 2010 Eugene Vorobkalo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Eugene Vorobkalo - initial API and implementation
 *     Andrey Fedorov - DocProductListAdapter
 ******************************************************************************/
package org.droidpres.adapter;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;
import org.droidpres.R;
import org.droidpres.activity.SetupRootActivity;

import java.text.DecimalFormat;

public class DocProductListAdapter extends SimpleCursorAdapter implements ViewBinder {
	public DecimalFormat nf;
	public DecimalFormat cf;
	private float mQty, mPrice;

	public DocProductListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
		super(context, layout, cursor, from, to);
		setViewBinder(this);

		nf = SetupRootActivity.getQtyFormat(context, context.getString(R.string.lb_qty));
		cf = SetupRootActivity.getCurrencyFormat(context);
	}

	
	public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

		switch (columnIndex) {

        case 0:
            mQty = cursor.getFloat(columnIndex);
            ((TextView) view).setText(nf.format(mQty));

			return true;
		case 1: // price
			mPrice = cursor.getFloat(columnIndex);
			((TextView) view).setText(" x "+cf.format(mPrice));

			return true;
        case 2:
            float LineTotal = cursor.getFloat(0) * cursor.getFloat(1);
            ((TextView) view).setText("= "+cf.format(LineTotal));

            return true;
		default:
            //Log.w("case default", "" + cursor.getString(3));

			return false;
		}
	}

}
